/*
 * @Author: LVGRAPE
 * @LastEditors: LVGRAPE
 */
#include "io.h"
#include "at32f4xx_rcc.h"
/**ZINO LITE ALL IOs */
zinoLiteIoList_t ZINO_IO_list = {
    .RX1 = {.port = GPIOA, .pins = GPIO_Pins_10, .mode = GPIO_Mode_IN_FLOATING, .speed = GPIO_MaxSpeed_50MHz, .name = "RX1"},
    .RX2 = {.port = GPIOA, .pins = GPIO_Pins_3, .mode = GPIO_Mode_IN_FLOATING, .speed = GPIO_MaxSpeed_50MHz, .name = "RX2"},
    .RX3 = {.port = GPIOB, .pins = GPIO_Pins_11, .mode = GPIO_Mode_IN_FLOATING, .speed = GPIO_MaxSpeed_50MHz, .name = "RX3"},
    .RX4 = {.port = GPIOF, .pins = GPIO_Pins_5, .mode = GPIO_Mode_IN_FLOATING, .speed = GPIO_MaxSpeed_50MHz, .name = "RX4"},
    .RX5 = {.port = GPIOD, .pins = GPIO_Pins_2, .mode = GPIO_Mode_IN_FLOATING, .speed = GPIO_MaxSpeed_50MHz, .name = "RX5"},
    .TX1 = {.port = GPIOA, .pins = GPIO_Pins_9, .mode = GPIO_Mode_AF_PP, .speed = GPIO_MaxSpeed_50MHz, .name = "TX1"},
    .TX2 = {.port = GPIOA, .pins = GPIO_Pins_2, .mode = GPIO_Mode_AF_PP, .speed = GPIO_MaxSpeed_50MHz, .name = "TX2"},
    .TX3 = {.port = GPIOB, .pins = GPIO_Pins_10, .mode = GPIO_Mode_AF_PP, .speed = GPIO_MaxSpeed_50MHz, .name = "TX3"},
    .TX4 = {.port = GPIOF, .pins = GPIO_Pins_4, .mode = GPIO_Mode_AF_PP, .speed = GPIO_MaxSpeed_50MHz, .name = "TX4"},
    .TX5 = {.port = GPIOC, .pins = GPIO_Pins_12, .mode = GPIO_Mode_AF_PP, .speed = GPIO_MaxSpeed_50MHz, .name = "TX5"},
    .SDA = {.port = GPIOF, .pins = GPIO_Pins_7, .mode = GPIO_Mode_OUT_OD, .speed = GPIO_MaxSpeed_50MHz, .name = "SDA"},
    .SCL = {.port = GPIOF, .pins = GPIO_Pins_6, .mode = GPIO_Mode_OUT_OD, .speed = GPIO_MaxSpeed_50MHz, .name = "SCL"},
    .OD1 = {.port = GPIOC, .pins = GPIO_Pins_6, .mode = GPIO_Mode_AF_PP, .speed = GPIO_MaxSpeed_50MHz, .name = "OD1"},
    .OD2 = {.port = GPIOC, .pins = GPIO_Pins_6, .mode = GPIO_Mode_AF_PP, .speed = GPIO_MaxSpeed_50MHz, .name = "OD2"},
    .OD3 = {.port = GPIOC, .pins = GPIO_Pins_6, .mode = GPIO_Mode_AF_PP, .speed = GPIO_MaxSpeed_50MHz, .name = "OD3"},
    .OD4 = {.port = GPIOC, .pins = GPIO_Pins_6, .mode = GPIO_Mode_AF_PP, .speed = GPIO_MaxSpeed_50MHz, .name = "OD4"},
    .MPU_RST = {.port = GPIOA, .pins = GPIO_Pins_15, .mode = GPIO_Mode_OUT_PP, .speed = GPIO_MaxSpeed_50MHz, .name = "MPU_RST"},
    .PWR_KEY = {.port = GPIOA, .pins = GPIO_Pins_0, .mode = GPIO_Mode_IN_FLOATING, .speed = GPIO_MaxSpeed_50MHz, .name = "PWR_KEY"},
    .PWR_CTRL = {.port = GPIOA, .pins = GPIO_Pins_1, .mode = GPIO_Mode_OUT_PP, .speed = GPIO_MaxSpeed_50MHz, .name = "PWR_CTR"},
    .PWR_ACT = {.port = GPIOA, .pins = GPIO_Pins_8, .mode = GPIO_Mode_OUT_PP, .speed = GPIO_MaxSpeed_50MHz, .name = "PWR_ACT"},
    .BLED = {.port = GPIOA, .pins = GPIO_Pins_11, .mode = GPIO_Mode_AF_OD, .speed = GPIO_MaxSpeed_50MHz, .name = "BLED"},
    .GLED = {.port = GPIOA, .pins = GPIO_Pins_12, .mode = GPIO_Mode_AF_OD, .speed = GPIO_MaxSpeed_50MHz, .name = "GLED"},
    .RLED = {.port = GPIOA, .pins = GPIO_Pins_13, .mode = GPIO_Mode_AF_OD, .speed = GPIO_MaxSpeed_50MHz, .name = "RLED"},
    .LED1 = {.port = GPIOB, .pins = GPIO_Pins_3, .mode = GPIO_Mode_IN_FLOATING, .speed = GPIO_MaxSpeed_50MHz, .name = "LED1"},
    .LED1 = {.port = GPIOB, .pins = GPIO_Pins_4, .mode = GPIO_Mode_IN_FLOATING, .speed = GPIO_MaxSpeed_50MHz, .name = "LED2"},
    .LED1 = {.port = GPIOB, .pins = GPIO_Pins_5, .mode = GPIO_Mode_IN_FLOATING, .speed = GPIO_MaxSpeed_50MHz, .name = "LED3"},
    .PWR_C1 = {.port = GPIOC, .pins = GPIO_Pins_0, .mode = GPIO_Mode_IN_ANALOG, .speed = GPIO_MaxSpeed_50MHz, .name = "PWR_C1"},
    .PWR_C2 = {.port = GPIOC, .pins = GPIO_Pins_1, .mode = GPIO_Mode_IN_ANALOG, .speed = GPIO_MaxSpeed_50MHz, .name = "PWR_C2"},
    .PWR_C3 = {.port = GPIOC, .pins = GPIO_Pins_2, .mode = GPIO_Mode_IN_ANALOG, .speed = GPIO_MaxSpeed_50MHz, .name = "PWR_C3"},
    .PWR_C4 = {.port = GPIOC, .pins = GPIO_Pins_3, .mode = GPIO_Mode_IN_ANALOG, .speed = GPIO_MaxSpeed_50MHz, .name = "PWR_C4"},
    .PWR_5V = {.port = GPIOA, .pins = GPIO_Pins_5, .mode = GPIO_Mode_IN_ANALOG, .speed = GPIO_MaxSpeed_50MHz, .name = "PWR_5V"},
    .PWR_VBAT = {.port = GPIOA, .pins = GPIO_Pins_4, .mode = GPIO_Mode_IN_ANALOG, .speed = GPIO_MaxSpeed_50MHz, .name = "PWR_BAT"},
};

static zino_io_t zinoIoInitDefault = {
    .port = GPIOA,
    .pins = GPIO_Pins_0,
    .speed = GPIO_MaxSpeed_50MHz,
    .mode = GPIO_Mode_IN_FLOATING,
};
static GPIO_InitType __GPIO_InitTypeDefault;
void zino_io_init(GPIO_Type *port,
                  uint16_t pins,
                  GPIOMaxSpeed_Type speed,
                  GPIOMode_Type mode)
{
    switch (port)
    {
    case GPIOA:
        RCC_APB2CLKConfig(RCC_APB2EN_GPIOAEN, ENABLE);
        break;
    case GPIOB:
        RCC_APB2CLKConfig(RCC_APB2EN_GPIOAEN, ENABLE);
        break;
    case GPIOC:
        RCC_APB2CLKConfig(RCC_APB2EN_GPIOAEN, ENABLE);
        break;
    case GPIOD:
        RCC_APB2CLKConfig(RCC_APB2EN_GPIOAEN, ENABLE);
        break;
    case GPIOF:
        RCC_APB2CLKConfig(RCC_APB2EN_GPIOAEN, ENABLE);
        break;
    default:
        return; // unknow port!
        break;
    }
    __GPIO_InitTypeDefault.GPIO_MaxSpeed = speed;
    __GPIO_InitTypeDefault.GPIO_Pins = pins;
    __GPIO_InitTypeDefault.GPIO_Mode = mode;
    GPIO_Init(port, &__GPIO_InitTypeDefault);
}

void zino_io_config(zino_io_t *io)
{
    zino_io_init(io->port, io->pins, io->speed, io->mode);
}
